/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.DAO;
import models.LoginStorage;
import views.AdminView;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import views.Popup;

/**
 *
 * @author Krzysiek
 */
public class ControllerAdmin {

    private LoginStorage model;
    private AdminView view;
    private DAO dao;
    private int currentTab;
    private Popup pop;

    public ControllerAdmin(LoginStorage model, AdminView view) throws SQLException {
        this.model = model;
        this.view = view;
        this.dao = new DAO();
        this.pop = new Popup();
        this.view.addAddButtonListener(new addButtonListener());
        this.view.addDeleteButtonListener(new deleteButtonListener());
        this.view.addModifyButtonListener(new modifyButtonListener());
        this.view.addJTabbedPaneListener(new jTabbedPaneListener());
        this.pop.UpdateListener(new updateButtonListener());
        this.view.setUsersTable(dao.getModelOf("Users"));
//        System.out.println("controller");
    }

    public void writeChosenRow() {
        String[] data = view.getSelectedRow();
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i] + " ");
        }
    }

    class addButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int tab = view.getActiveTab();
            int number = 0;
            if (tab == 0) {
                number = 6;
            } else {
                number = 2;
            }
            pop.setPopupWindow(number, tab);
        }
    }

    class deleteButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int tab = view.getActiveTab();
            String[] data = view.getSelectedRow();
            String table = null;
            switch (tab) {
                case 0:
                    table = "Users";
                    break;
                case 1:
                    table = "AirConditioning";
                    break;
                case 2:
                    table = "Heaters";
                    break;
                case 3:
                    table = "Lights";
                    break;
            }
            try {
                dao.deleteFrom(Integer.parseInt(data[0]), table);
            } catch (SQLException ex) {
//                Logger.getLogger(ControllerAdmin.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Deleting Failed");
            }
//            System.out.println("deleteButton");
        }
    }

    class modifyButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
//            writeChosenRow();
            int tab = view.getActiveTab();
            int number = 0;
            if (tab == 0) {
                number = 7;
            } else {
                number = 3;
            }
            pop.setPopupWindow(number, tab, view.getSelectedRow());
        }
    }

    class jTabbedPaneListener implements ChangeListener {

        @Override
        public void stateChanged(ChangeEvent e) {
            JTabbedPane pane = (JTabbedPane) e.getSource();
            currentTab = pane.getSelectedIndex();
            switch (currentTab) {
                case 0:
                    try {
                        view.setUsersTable(dao.getModelOf("Users"));
                    } catch (SQLException ex) {
                        Logger.getLogger(ControllerAdmin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                case 1:
                    try {
                        view.setACTable(dao.getModelOf("AirConditioning"));
                    } catch (SQLException ex) {
                        Logger.getLogger(ControllerAdmin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                case 2:
                    try {
                        view.setHeatersTable(dao.getModelOf("Heaters"));
                    } catch (SQLException ex) {
                        Logger.getLogger(ControllerAdmin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                case 3:
                    try {
                        view.setLightsTable(dao.getModelOf("Lights"));
                    } catch (SQLException ex) {
                        Logger.getLogger(ControllerAdmin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
            }
        }
    }

    public class updateButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            addUpdate();
        }
    }

    public void addUpdate() {
        String[] data = pop.getFieldsData();
        int tab = view.getActiveTab();
        if (data.length == 7 || data.length == 3) {
            switch (tab) {
                case 0:
                    try {
                        dao.updateTable("Users", Integer.parseInt(data[0].toString()), data[1].toString(), data[2].toString(),
                                Integer.parseInt(data[3].toString()), Integer.parseInt(data[4].toString()),
                                Integer.parseInt(data[5].toString()), data[6].toString());
                    } catch (SQLException ex) {
                        Logger.getLogger(ControllerAdmin.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    break;
                case 1:
                    try {
                        dao.updateTable("AirConditioning", Integer.parseInt(data[0].toString()), data[1].toString(), Integer.parseInt(data[2].toString()));
                    } catch (SQLException ex) {
                        Logger.getLogger(ControllerAdmin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                case 2:
                    try {
                        dao.updateTable("Heaters", Integer.parseInt(data[0].toString()), data[1].toString(), Integer.parseInt(data[2].toString()));
                    } catch (SQLException ex) {
                        Logger.getLogger(ControllerAdmin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                case 3:
                    try {
                        dao.updateTable("Lights", Integer.parseInt(data[0].toString()), data[1].toString(), Integer.parseInt(data[2].toString()));
                    } catch (SQLException ex) {
                        Logger.getLogger(ControllerAdmin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
            }
        } else {
            switch (tab) {
                case 0:
                    try {
                        dao.addData("Users", data[0].toString(), data[1].toString(),
                                Integer.parseInt(data[2].toString()), Integer.parseInt(data[3].toString()),
                                Integer.parseInt(data[4].toString()), data[5].toString());
                    } catch (SQLException ex) {
                        Logger.getLogger(ControllerAdmin.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    break;
                case 1:
                    try {
                        dao.addData("AirConditioning", data[0].toString(), Integer.parseInt(data[1].toString()));
                    } catch (SQLException ex) {
                        Logger.getLogger(ControllerAdmin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                case 2:
                    try {
                        dao.addData("Heaters", data[0].toString(), Integer.parseInt(data[1].toString()));
                    } catch (SQLException ex) {
                        Logger.getLogger(ControllerAdmin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                case 3:
                    try {
                        dao.addData("Lights", data[0].toString(), Integer.parseInt(data[1].toString()));
                    } catch (SQLException ex) {
                        Logger.getLogger(ControllerAdmin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
            }
        }
        pop.setVisible(false);
    }
}
